from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from Func_Extract_Data import Extract_Track_Data, Extract_Coordinates_2
from wrf import (get_cartopy,cartopy_xlim, cartopy_ylim, latlon_coords)
from cartopy.feature import NaturalEarthFeature
from mpl_toolkits.basemap import Basemap
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
import cartopy.crs as crs
import numpy as np
import os

#----------------------------------------------------------------------------------------------
plt.rcParams.update({
    "text.usetex": True,
    "font.family": "sans-serif",
    "font.sans-serif": ["Helvetica"]})
# for Palatino and other serif fonts use:
plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "font.serif": ["Palatino"],
})

#----------------------------------------------------------------------------------------------
Input_Dir   = '.../Post_Processing/Track/' # Provide the input directory
HNS         = ['Florence']
HRS         = ['96']
GS          = '8km'
TMS         = ['NoTurb']
CLTS        = ['TSKM2', 'Default', 'TSKP2']
#--------------------------------------------------------------------

fig, ax     = plt.subplots(figsize=(10,10))
colors      = ['royalblue', 'green', 'red']
markers     = ['^', 'p', 'd']
lines       = ['--', ':', '.-']

min_lat     = 13
max_lat     = 46
max_lon     = -55
min_lon     = -81
#--------------------------------------------------------------------

idx = 0
for HN in HNS:
    m = Basemap(projection='merc', llcrnrlat=min_lat,urcrnrlat=max_lat, llcrnrlon=min_lon,urcrnrlon=max_lon, 
        resolution= 'c' , lat_0=47, lon_0=-100, ax = ax)
    m.drawstates()
    meridians = [i for i in range(min_lon, max_lon, 10)]
    parallels = [i for i in range(min_lat, max_lat, 10)]
    m.drawmeridians(meridians, color='k', textcolor='k', linewidth=1.5,
        zorder=None, dashes=[6, 1000], labels=[1, 0, 0, 1], labelstyle=None, fmt='%g', xoffset=None, 
        yoffset=None, ax=None, latmax=None, fontsize=14)
    m.drawparallels(parallels, color='k', textcolor='k', linewidth=1.5, zorder=None, dashes=[6, 1000], 
        labels=[1, 0, 0, 1], labelstyle=None, fmt='%g', xoffset=None, yoffset=None, ax=None, latmax=None, fontsize=14)
    m.drawcoastlines(linewidth=0.7, linestyle='solid', color='grey')
    m.drawcountries()
    m.shadedrelief()
    m.drawmapboundary()

    c = 0
    for CLT in CLTS:
        Hurricane_Setting = HN + '_' + HRS[idx] + 'Hours_' + GS + '_' + TMS[0] + '_' + CLT + '.csv'
        print (Hurricane_Setting)
        Eye_Lats  = []
        Eye_Longs = []

        (Eye_Lats, Eye_Longs) = Extract_Coordinates_2 (Input_Dir, Hurricane_Setting,'min_lat', 'min_long')
        x, y = m(Eye_Longs, Eye_Lats)
        ax.plot(x, y, color=colors[c], linewidth=1.5, linestyle='--', #marker = markers[i],
                label = GS)
        ax.scatter(x, y, color=colors[c], marker = markers[c], s=20)
        
        Real_Lats  = []
        Real_Longs = []
        Real_Lats  = Extract_Track_Data (Input_Dir, 'Real_Output.csv', Real_Lats, 'Real_lat')
        Real_Longs = Extract_Track_Data (Input_Dir, 'Real_Output.csv', Real_Longs, 'Real_long')
        x, y = m(Real_Longs[:], Real_Lats[:])
        ax.scatter(x[:], y[:], color='black')
        ax.plot(x[:], y[:], color='black', linewidth=2, label = 'Best Track')
        ax.set_title(HN, {'size': 16})
        c += 1
    idx += 1

#----------------------------------------------------------------------------------------------
black_line = mlines.Line2D([], [], color='black', marker= 'o', markersize=5)
royalblue_line = mlines.Line2D([], [], color='royalblue', linestyle='--', marker= '^', markersize=5)
red_line = mlines.Line2D([], [], color='red', linestyle='--', marker= 'p', markersize=5)
green_line = mlines.Line2D([], [], color='green', linestyle='--', marker= '^', markersize=5)

fig.legend(handles = [red_line, green_line, royalblue_line, black_line], 
labels=['$\mathit{TSK}$'+r'$_{+ 2K}$', '$\mathit{Default}$', '$\mathit{TSK}$'+r'$_{- 2K}$', "Best Track"], 
bbox_to_anchor=(0.7, 0.99, -0.4, 0), loc = 'upper center', frameon = False,
mode='expand', ncol = 4, fontsize='xx-large')
plt.show()

